package com.foodost.idiomer;

import net.youmi.android.AdManager;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;

public class SplashActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        AdManager.getInstance(this).init("fcee3d49887856e3", "5f48cd0b2e767816", false);
        if(!Utils.getInitShortCut(this)){
            addShortcut();
        }
        int play = Utils.getPlaysSetting(this);
        if(isDataInited()){
        	if(play == 1){
        		Intent in = new Intent();
        		in.setClass(this, MainActivity.class);
        		startActivity(in);
        	}else if(play == 2){
        		Intent in = new Intent();
        		in.setClass(this, CavilOneActivity.class);
        		startActivity(in);
        	}
        }
    }
    
    private boolean isDataInited(){
        SharedPreferences sf = getSharedPreferences(Constants.DB_SHARED, 0);
        boolean isInited = sf.getBoolean(Constants.IS_INITDATA, false);
        return isInited;
    }
    
    @Override
    protected void onRestart() {
        super.onRestart();
        finish();
    }
    
    private boolean hasShortcut()
    {
            boolean isInstallShortcut = false;
            final ContentResolver cr = this.getContentResolver();
            final String AUTHORITY ="com.android.launcher.settings";
            final Uri CONTENT_URI = Uri.parse("content://" +AUTHORITY + "/favorites?notify=true");
            Cursor c = cr.query(CONTENT_URI,new String[] {"title","iconResource" },"title=?",
            new String[] {this.getString(R.string.app_name).trim()}, null);
            if(c!=null && c.getCount()>0){
                isInstallShortcut = true ;
            }
            return isInstallShortcut ;
        }
    
    private void addShortcut(){  
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
               
        //快捷方式的名称  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));  
        shortcut.putExtra("duplicate", false); //不允许重复创建  
 
       Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
       shortcutIntent.setClassName(this, this.getClass().getName());
       shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        //快捷方式的图标  
        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.idiom_icon);  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);  
        sendBroadcast(shortcut);
        Utils.setInitShortCut(this, true);
    }

}
